package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Intent
import android.os.Bundle

import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter4.BaseQuickAdapter
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityExpenseSelectionBinding
import com.haoshuang.zhouzhoubang.databinding.ItemExpenseBinding
import com.haoshuang.zhouzhoubang.model.bean.PriceBean
import com.haoshuang.zhouzhoubang.ui.vm.ExpenseSelectionViewModel
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.visibleOrInvisible


/**
 *  费用选择界面
 */
class ExpenseSelectionActivity : BaseVMActivity<ExpenseSelectionViewModel, ActivityExpenseSelectionBinding>() {
    var list= arrayListOf<PriceBean>()
    /**recycleview列表适配器*/
    private val mrvAdapter by lazy {
        object : BaseBindingQuickAdapter<PriceBean, ItemExpenseBinding>(list){

            override fun onBindViewHolder(
                holder: BaseBindingHolder,
                position: Int,
                item: PriceBean?
            ) {
                val binding = ItemExpenseBinding.bind(holder.itemView)
                binding.apply {
                    tvExpense.text = item?.name
                    if (item?.isCheck==0){
                        ivSelect.visibleOrInvisible(false)
                    }else{
                        ivSelect.visibleOrInvisible(true)
                    }

                }
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        val layoutManager = LinearLayoutManager(this)	// 创建布局管理者
        binding.apply {
            appBar.ivBack.clickWithTrigger { finish() }
            appBar.tvTitle.text = "费用选择"
            tvCommit.clickWithTrigger {
                if (   bean2==null){
                    toast("请选择后在确认")
                    return@clickWithTrigger
                }
                val intt= Intent()

                intt.putExtra("name",bean2?.name)
                intt.putExtra("money",bean2?.money)
                intt.putExtra("id",bean2?.id)

                setResult(88,intt)
                finish()
            }
            recyclerView.layoutManager = layoutManager	// 给recycleview设置布局管理者
            recyclerView.adapter = mrvAdapter	// 给recycleview设置适配器对象
            mrvAdapter.setEmptyData()
            mrvAdapter.isStateViewEnable = true
            mrvAdapter.setOnItemClickListener { _, _, position ->
                val bean=list[position]
                if (bean.isCheck==0){
                    for (b in list){
                        b.isCheck=0
                    }
                    bean.isCheck=1
                    bean2=bean
                }else{
                    bean.isCheck=0
                    bean2=null
                }
                mrvAdapter.notifyDataSetChanged()
            }
       }


    }
    var ids=0
    private var bean2:PriceBean?=null
    override fun initData() {
        ids=intent.getIntExtra("ids",0)
        vm.getData("id" to ids)

    }

    override fun startObserve() {
        vm.getData.observeState(this) {
                    onLoading { showLoading() }
                    onError { code, msg ->
                        dismissLoading()
                        if (code!="402"){
                            toast(msg)
                        }
                    }
                    onSuccess {
                        dismissLoading()
                        list.clear()
                        list.addAll(it!!)
                        mrvAdapter.notifyDataSetChanged()
                    }
                }
    }

}